@isTest
private class OppStageChangeCheckNoteTest {

    // 终端客户业务机会
    static testMethod void testMethod1() {
        RecordType rt = [select Id from RecordType where SObjectType = 'Account' and DeveloperName = 'TerminalCustomer'];
        RecordType mrt = [select Id from RecordType where SObjectType = 'Account' and DeveloperName = 'Maccura'];

        RecordType rtopp = [select Id from RecordType where SObjectType = 'Opportunity' and DeveloperName = 'CustomerOpportunity'];

        Province__c pro = TestUtils.createProvince();
        insert pro;

        City__c c = TestUtils.createCity();
        c.Province__c = pro.Id;
        insert c;

        District__c d = TestUtils.createDistrict();
        d.City__c = c.Id;
        insert d;

        // 客户
        Account acc = new Account();
        acc.Name = 'test customer';
        acc.Status__c = '审核通过';
        acc.RecordTypeId = rt.Id;
        insert acc;

          // 客户
        Account acc1 = new Account();
        acc1.Name = 'mac';
        acc1.Status__c = '审核通过';
        acc1.RecordTypeId = mrt.Id;
        insert acc1;

        Set<Id> accSet = new Set<Id>();
        accSet.add(acc.Id);
        accSet.add(acc1.Id);


         Product__c p1 = TestUtils.createProduct();        
        p1.Category1__c = '微生物'; 
        p1.Purpose__c = '仪器报价用';
        p1.RecordTypeId = utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        insert p1 ;


        List<Department__c> dts = [select Id from Department__c where TerminalCustomer__c in:accSet ];
        for (Department__c dt: dts) {
            dt.LastYearRevenue__c = 11;
            dt.PurchasingCost__c = 11;
        }
        update dts;


        // 业务机会
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        opp.StageCanChange__c = '意向';
        opp.StageName = '意向';
        opp.Department__c = '';
        insert opp;

        //-------------------测试立项check-----------------------
        try {
            opp.StageName = '立项';
            update opp;
        } catch (Exception e) {}

        // 补充客户信息
        acc.Phone = '123456';
        acc.Level__c = '一级医院';
        acc.Category__c = '综合医院';
        acc.CustomerAttribute__c = '公立医疗机构';
        acc.Address__c = '123456';
        acc.Province__c = pro.Id;
        acc.City__c = c.Id;
        acc.District__c = d.Id;
        acc.Postcode__c = '610000';
        acc.BedNumOpen__c = 100;
        acc.SpecialDepart__c = '内科_呼吸内科';
        acc.LastYearRevenue__c = 5000;
        update acc;

        // 意向产品
        IntentProduct__c ip = TestUtils.createIntentProduct();
        ip.Opportunity__c = opp.Id;
        ip.Product__c = p1.Id; 

        insert ip;

        // 联系人补充
        Contact con1 = TestUtils.createContact();
        con1.LastName = '测试采购科';
        con1.AccountId = acc.Id;
        insert con1;

        Contact con2 = TestUtils.createContact();
        con2.LastName = '测试分管院长';
        con2.Post__c = '分管院长(设备)';
        con2.Departments__c = '医院办公室';
        con2.AccountId = acc.Id;
        insert con2;

        Contact con3 = TestUtils.createContact();
        con3.LastName = '测试财务';
        con3.Post__c = '科长/主任';
        con3.Departments__c = '财务科/计财处';
        con3.AccountId = acc.Id;
        insert con3;

        Contact con4 = TestUtils.createContact();
        con4.LastName = '测试检验';
        con4.Post__c = '科长/主任';
        con4.Departments__c = '检验科';
        con4.AccountId = acc.Id;
        insert con4;

        try {
            opp.StageName = '立项';
            opp.Department__c = '检验科;输血科';
            update opp;
        } catch (Exception e) {}
    
        // 科室上年度收入
        Department__c dept = [select Id from Department__c where TerminalCustomer__c = :acc.Id];
        dept.LastYearRevenue__c = 1000;
        update dept;

        opp.StageName = '立项';
        opp.Department__c = '检验科';
        opp.ExecutiveCompany__c =acc1.Id;
        opp.ExecuteCompany__c= acc1.Id;
        update opp;
    }

    // 经销商业务机会
    static testMethod void testMethod2() {
        RecordType rt = [select Id from RecordType where SObjectType = 'Account' and DeveloperName = 'Dealer'];
        RecordType rtopp = [select Id from RecordType where SObjectType = 'Opportunity' and DeveloperName = 'DealerOpportunity'];

        Province__c pro = TestUtils.createProvince();
        insert pro;

        City__c c = TestUtils.createCity();
        c.Province__c = pro.Id;
        insert c;

        District__c d = TestUtils.createDistrict();
        d.City__c = c.Id;
        insert d;

        // 客户
        Account acc = new Account();
        acc.Name = 'test dealer';
        acc.Status__c = '审核通过';
        acc.RecordTypeId = rt.Id;
        insert acc;

        // 业务机会
        Opportunity opp = new Opportunity();
        opp.Name = 'test dealer opp';
        opp.RecordTypeId = rtopp.Id;
        opp.AccountId = acc.Id;
        opp.StageName = '意向';
        opp.CloseDate = Date.today().addDays(180);
        insert opp;

        try {
            opp.StageName = '方案谈判';
            update opp;
        } catch (Exception e) {}

        // 补充客户信息
        Contact con = new Contact(LastName = 'Bob', AccountId = acc.Id);
        insert con;

        acc.CusType__c = '经营企业';
        acc.Artificial__c = 'Bob';
        acc.Phone = '123456';
        acc.Address__c = '123456';
        acc.RegisterAmount__c = 5000;
        acc.Province__c = pro.Id;
        acc.City__c = c.Id;
        acc.District__c = d.Id;
        acc.Postcode__c = '610000';
        acc.BusinessLicenseEndDate__c = Date.today().addDays(360);
        acc.Company__c = 'test';
        acc.LastYearRevenue__c = 6000;
        acc.Actual_Controller__c = con.Id;
        acc.Master_Shareholder__c = con.Id;
        update acc;

        
    }

    // 海外经销商业务机会
    static testMethod void testMethod3() {
        RecordType rt = [select Id from RecordType where SObjectType = 'Account' and DeveloperName = 'DealerAbroad'];
        RecordType rtopp = [select Id from RecordType where SObjectType = 'Opportunity' and DeveloperName = 'OverseaDearlerOpportunity'];

        // 客户
        Account acc = new Account();
        acc.Name = 'test dealer';
        acc.Status__c = '审核通过';
        acc.RecordTypeId = rt.Id;
        insert acc;

        // 业务机会
        Opportunity opp = new Opportunity();
        opp.Name = 'test dealer opp';
        opp.RecordTypeId = rtopp.Id;
        opp.AccountId = acc.Id;
        opp.StageName = '取得联系';
        opp.CloseDate = Date.today().addDays(180);
        insert opp;

        try {
            opp.StageName = '市场沟通';
            update opp;
        } catch (Exception e) {}

        Attachment att = new Attachment();
        att.Name = '竞争对手价格123456';
        att.ContentType = 'txt/plain';
        att.Body = Blob.valueOf('1234567890');
        att.ParentId = opp.Id;
        insert att;

    }
}